﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adams.Public.Web.Business
{
    public static class Security
    {
        public static Adams.Public.DataTransfer.Security.LoginOutput TryLogin(DataTransfer.Security.LoginInput input)
        {
            var model = Api.GetData<Adams.Public.DataTransfer.Security.LoginOutput>("Security/TryLogin", input);
            return model;
        }

        public static DataTransfer.Security.ForgotOutput TryForgot(DataTransfer.Security.ForgotInput input)
        {
            var model = Api.GetData<Adams.Public.DataTransfer.Security.ForgotOutput>("Security/TryForgot", input);
            return model;
        }

        public static Adams.Public.DataTransfer.Security.SignupOutput TrySignup(Adams.Public.DataTransfer.Security.SignupInput input)
        {
            var model = Api.GetData<Adams.Public.DataTransfer.Security.SignupOutput>("Security/TrySignup", input);
            return model;
        }

        public static DataTransfer.Security.SignupCompleteOutput TrySignupComplete(DataTransfer.Security.SignupCompleteInput input)
        {
            var model = Api.GetData<Adams.Public.DataTransfer.Security.SignupCompleteOutput>("Security/TrySignupComplete", input);
            return model;
        }

        public static Adams.Public.DataTransfer.Security.PasswordResetOutput TryPasswordReset(DataTransfer.Security.PasswordResetInput input)
        {
            var model = Api.GetData<Adams.Public.DataTransfer.Security.PasswordResetOutput>("Security/TryPasswordReset", input);
            return model;
        }

        public static DataTransfer.Security.ResendOutput TryResend(DataTransfer.Security.ResendInput input)
        {
            var model = Api.GetData<Adams.Public.DataTransfer.Security.ResendOutput>("Security/TryResend", input);
            return model;
        }

        public static string GenerateCaptcha(string randomText)
        {
            string code = Shared.Crypt.Encrypt(randomText + "|" + DateTime.UtcNow.ToString());
            return code;
        }

        public static bool ValidateCaptcha(string input, string code)
        {
            string en = Shared.Crypt.Decrypt(code);
            string[] items = en.Split('|');
            var text = items[0];
            DateTime d = DateTime.Parse(items[1]);

            if (Shared.Str.IsEqual(text, input))
            {
                if(Shared.DateHelper.DiffMins(d, DateTime.UtcNow) < 420)
                {
                    return true;
                }
            }

            return false;
        }
    }
}
